﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace IPCByMessages
{
    public class SyncClientMesagesChannel : SyncMessagesChannelBase, IMessagesChannel
    {
        /// <summary>
        /// Gets whether current IMessagesChannel realization is server or client.
        /// For that class (SyncClientMesagesChannel) it's always false.
        /// </summary>
        public bool IsServerSide { get { return false; } }

        /// <summary>
        /// Creates new instance of channel.
        /// Tries to connect to opened at remote host socket
        /// </summary>
        /// <param name="host">Hostname to connect</param>
        /// <param name="port">Port to connect</param>
        internal SyncClientMesagesChannel(string host, int port)
        {
            try
            {
                connection = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,
                    ProtocolType.Tcp);

                connection.Connect(host, port);

                connection.SendBufferSize =
                    connection.ReceiveBufferSize = 655360;
                 
                otherSide = connection; //to do it in common way
                Active = true;
                BeginReceive();
            }
            catch
            {
                Close();
                throw;
            }
        }
    }
}
